package me.numark.utils;

import com.google.gson.Gson;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import me.numark.NumarkApplication;
import me.numark.model.User;
import me.numark.providers.Numa;

/**
 * Created by tian on 14-9-17:上午7:43.
 */
public class Settings {

    public static final String PREF_NAME = "preferences";

    private static final String TOKEN_KEY = "token";

    static Context gContext;

    public static SharedPreferences getPrefs() {
        if (gContext == null) {
            gContext = NumarkApplication.getInstance();
        }
        int code = Context.MODE_MULTI_PROCESS;
        return gContext.getSharedPreferences(PREF_NAME, code);
    }

    private static final String WEIBO_UID_KEY = "weibo_uid";

    private static final String QQ_KEY = "qq";

    private static final String WEIBO_DISPLAY_NAME = "weibo_display_name";

    private static final String QQ_DISPLAY_NAME = "qq_display_name";

    private static final String QQ_UID_KEY = "qq_uid";

    private static final String HX_TOKEN_KEY = "hx-key";

    public static final String HX_NAME_KEY = "hx-name";

    private static final String HX_AUTHOR_KEY = "hx-time";


    public static void saveLogInInfo(String token, String name, String authorKey) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(HX_TOKEN_KEY, token);
        editor.putString(HX_NAME_KEY, name);
        editor.putString(HX_AUTHOR_KEY, authorKey);
        editor.apply();
    }

    public static void saveLogInInfo(String uid, String token) {
        SharedPreferences.Editor edit = getPrefs().edit();
        edit.putString(UID_KEY, uid);
        edit.putString(TOKEN_KEY, token);
        edit.apply();
    }

    private static final String MOBILE_KEY = "mobile";

    public static void saveMobile(String mobile) {
        getPrefs().edit().putString(MOBILE_KEY, mobile).apply();
    }

    private static final String UID_KEY = "uid";

    public static void saveUid(String uid) {
        getPrefs().edit().putString(UID_KEY, uid).apply();
    }

    private static final String NICK_KEY = "nick";

    public static void saveNick(String nick) {
        getPrefs().edit().putString(NICK_KEY, nick).apply();
    }

    public static String getNick() {
        return getPrefs().getString(NICK_KEY, "");
    }

    private static final String DESCRIPTION_KEY = "description";

    public static void saveDescription(String description) {
        getPrefs().edit().putString(DESCRIPTION_KEY, description).apply();
    }

    public static String getDescription() {
        return getPrefs().getString(DESCRIPTION_KEY, "");
    }

    private static final String AVATAR = "avatar";

    public static void setAvatar(String avatar) {
        getPrefs().edit().putString(AVATAR, avatar).apply();
    }

    public static String getAvatar() {
        return getPrefs().getString(AVATAR, "");
    }

    private static final String SHOW_GUIDE = "showGuideV1";

    public static boolean needShowGuide() {
        SharedPreferences pref = getPrefs();
        boolean needShow = pref.getBoolean(SHOW_GUIDE, true);
        if (needShow) {
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean(SHOW_GUIDE, false);
            editor.apply();
        }
        return needShow;
    }

    private static final String FIRST_LAUNCH_KEY = "first_launch";

    public static boolean isFirstLaunch() {
        SharedPreferences pref = getPrefs();
        boolean isFirst = pref.getBoolean(FIRST_LAUNCH_KEY, true);
        if (isFirst) {
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean(FIRST_LAUNCH_KEY, false);
            editor.apply();
        }
        return isFirst;
    }

    private static final String SINA_WEIBO_TOKEN = "sina_weibo_token";

    private static final String SINA_WEIBO_EXPIRE_TIME = "sina_weibo_expire_time";

    private static final String QQ_TOKEN = "qq_token";

    private static final String QQ_EXPIRE_TIME = "qq_expire_time";

    public static void saveWeiboAccessToken(Oauth2AccessToken accessToken) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(SINA_WEIBO_TOKEN, accessToken.getToken());
        editor.putLong(SINA_WEIBO_EXPIRE_TIME, accessToken.getExpiresTime());
        editor.apply();
    }

    public static Oauth2AccessToken readWeiboAccessToken() {
        Oauth2AccessToken token = new Oauth2AccessToken();
        SharedPreferences pref = getPrefs();
        token.setToken(pref.getString(SINA_WEIBO_TOKEN, ""));
        token.setExpiresTime(pref.getLong(SINA_WEIBO_EXPIRE_TIME, 0));
        return token;
    }

    public static void saveQQAccessToken(Oauth2AccessToken accessToken) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(QQ_TOKEN, accessToken.getToken());
        editor.putLong(QQ_EXPIRE_TIME, accessToken.getExpiresTime());
        editor.apply();
    }

    public static Oauth2AccessToken readQQAccessToken() {
        Oauth2AccessToken token = new Oauth2AccessToken();
        SharedPreferences pref = getPrefs();
        token.setToken(pref.getString(QQ_TOKEN, ""));
        token.setExpiresTime(pref.getLong(QQ_EXPIRE_TIME, 0));
        return token;
    }

    public static boolean isLoggedIn() {
        String userInfo = getProfile();
        if (!TextUtils.isEmpty(userInfo)) {
            User user = new Gson().fromJson(userInfo, User.class);
            return user.status == 0;
        }
        return false;
    }

    public static String getToken() {
        return getPrefs().getString(TOKEN_KEY, "");
    }

    public static void saveToken(String token) {
        getPrefs().edit().putString(TOKEN_KEY, token).apply();
    }

    public static String getUid() {
        return getPrefs().getString(UID_KEY, "");
    }

    public static void saveWeiboUid(String id) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(WEIBO_UID_KEY, id);
        editor.apply();
    }

    public static String getWeiboUid() {
        return getPrefs().getString(
                WEIBO_UID_KEY, "");
    }

    public static void saveQQLogInfo(String info) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(QQ_KEY, info);
        editor.apply();
    }

    public static String getQQLogInfo() {
        return getPrefs().getString(
                QQ_KEY, "");
    }

    public static String getWeiboName() {
        return getPrefs().getString(
                WEIBO_DISPLAY_NAME, null);
    }

    public static void setWeiboName(String text) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(WEIBO_DISPLAY_NAME, text);
        editor.apply();
    }

    public static String getQQName() {
        return getPrefs().getString(
                QQ_DISPLAY_NAME, null);
    }

    public static void setQQName(String text) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(QQ_DISPLAY_NAME, text);
        editor.apply();
    }

    public static void saveQQUid(String id) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(QQ_UID_KEY, id);
        editor.apply();
    }

    public static String getQQUid() {
        return getPrefs().getString(QQ_UID_KEY, "");
    }

    public static void clearLogInInfo() {
        SharedPreferences preferences = getPrefs();
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(HX_NAME_KEY);
        editor.remove(HX_AUTHOR_KEY);
        editor.remove(HX_TOKEN_KEY);
        editor.remove(PROFILE);
        editor.apply();
    }

    public static void clearQQLogInInfo() {
        SharedPreferences.Editor editor = getPrefs().edit();
        editor.remove(QQ_DISPLAY_NAME);
        editor.remove(QQ_EXPIRE_TIME);
        editor.remove(QQ_KEY);
        editor.remove(QQ_TOKEN);
        editor.remove(QQ_UID_KEY);
        editor.apply();
    }

    public static void clearSinaLogInInfo() {
        SharedPreferences.Editor editor = getPrefs().edit();
        editor.remove(SINA_WEIBO_EXPIRE_TIME);
        editor.remove(SINA_WEIBO_TOKEN);
        editor.remove(WEIBO_DISPLAY_NAME);
        editor.remove(WEIBO_UID_KEY);
        editor.apply();
    }

    public static void logOut() {
        clearLogInInfo();
        clearQQLogInInfo();
        clearSinaLogInInfo();
        getPrefs().edit().clear().apply();
        NumarkApplication.getInstance().getContentResolver()
                .delete(Numa.NewsColumns.CONTENT_URI, null, null);
        NumarkApplication.getInstance().getContentResolver()
                .delete(Numa.FeedColumns.CONTENT_URI, null, null);
        NumarkApplication.getInstance().getContentResolver()
                .delete(Numa.DialogColumns.CONTENT_URI, null, null);
        NumarkApplication.getInstance().getContentResolver()
                .delete(Numa.MessageColumns.CONTENT_URI, null, null);
        NumarkApplication.getInstance().getContentResolver()
                .delete(Numa.UserColumns.CONTENT_URI, null, null);
        NumarkApplication.getInstance().getContentResolver()
                .delete(Numa.KeyValueColumns.CONTENT_URI, null, null);
    }

    public static final String PROFILE = "profile";

    public static void saveProfile(String profile) {
        getPrefs().edit().putString(PROFILE, profile).apply();
    }

    public static String getProfile() {
        return getPrefs().getString(PROFILE, "");
    }
}
